package org.batatasframework.chart.data;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.batatasframework.chart.columns.ChartColumn;

public class ChartExportResolver extends AbstractChartResolver{
	private final XSSFWorkbook workbook;
	private final XSSFSheet sheet;

	public ChartExportResolver(ChartData data, XSSFWorkbook workbook) {
		super(data);
		this.workbook = workbook;
		this.sheet = workbook.createSheet();
	}
	
	public void write(HttpServletResponse response) throws Exception{
		String names = definition.getName() + ".xlsx";
		response.setHeader("Content-disposition", "attachment;filename=" + new String(names.getBytes("gb2312"), "ISO8859-1"));
		workbook.write(response.getOutputStream());
	}
	
	@Override
	protected void internalInitialize() {
		XSSFCellStyle headStyle = workbook.createCellStyle();
		headStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		headStyle.setAlignment(HorizontalAlignment.CENTER_SELECTION);
		headStyle.setWrapText(true);//自动换行
		
		XSSFCellStyle bodyStyle = workbook.createCellStyle();
		bodyStyle.setVerticalAlignment(VerticalAlignment.CENTER);
		bodyStyle.setAlignment(HorizontalAlignment.LEFT);
		bodyStyle.setWrapText(true);//自动换行
		
		XSSFRow[] heads = new XSSFRow[headRowCount];
		int currentRow = 0;
		for(; currentRow < heads.length; currentRow++){
			heads[currentRow] = __row(currentRow);
		}
		
		//处理序号这个列
		XSSFCell cell = heads[0].createCell(0);
		cell.setCellStyle(headStyle);
		cell.setCellValue("序号");
		if(headRowCount > 1){
			sheet.addMergedRegion(new CellRangeAddress(0, headRowCount, 0, 0));
		}
		
		axis.resolveHead(heads, headStyle, 0);
		for(ChartColumn column : definition){
			column.resolveHead(heads, headStyle, 0);
		}
		
		//数据内容区域
		XSSFRow row = __row(currentRow);
		cell = row.createCell(0);
		cell.setCellStyle(bodyStyle);
		cell.setCellValue("合计：");
		int size = axis.size();
		sheet.addMergedRegion(new CellRangeAddress(currentRow, currentRow, 0, size++));
		for(Object obj : data.summary){
			cell = row.createCell(size++);
			cell.setCellStyle(bodyStyle);
			cell.setCellValue(obj.toString());
		}
		
		int ordinal = 1;
		currentRow += 1;//下一个可用的row
		for(Object[] objs : data.data){
			row = __row(currentRow++);
			size = 0;
			cell = row.createCell(size++);
			cell.setCellStyle(bodyStyle);
			cell.setCellValue(ordinal++);
			for(Object obj : objs){
				cell = row.createCell(size++);
				cell.setCellStyle(bodyStyle);
				cell.setCellValue(obj.toString());
			}
		}
	}

	private XSSFRow __row(int rowIndex) {
		XSSFRow sheetRow = sheet.getRow(rowIndex);
		if (sheetRow == null) {
			sheetRow = sheet.createRow(rowIndex);
		}
		return sheetRow;
	}
	
}
